package com.difsonapps.android.marker.upload;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.difsonapps.android.marker.IRefresher;

import android.util.Log;

public class HttpUploader {
	private static final String FILE_PART_NAME = "file";
	String url;
	String filePath;
	IRefresher<String> successListener;
	IRefresher<String> errorListener;
	
	
	
	public HttpUploader(String url, String filePath,
			IRefresher<String> successListener, IRefresher<String> errorListener) {
		super();
		this.url = url;
		this.filePath = filePath;
		this.successListener = successListener;
		this.errorListener = errorListener;
	}



	public void upload() throws Exception {
		HttpClient client = new DefaultHttpClient();
	    HttpPost post = new HttpPost(url);
	    try {
	      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
	      nameValuePairs.add(new BasicNameValuePair("registrationid",
	          "123456789"));
	      post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	 
	      HttpResponse response = client.execute(post);
	      BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
	      String line = "";
	      while ((line = rd.readLine()) != null) {
	        System.out.println(line);
	        Log.d("Line", line);
	      }

	    } catch (IOException e) {
	      e.printStackTrace();
	    }
		if(true) return;
        //Url of the server
        //String url = "";
//        HttpClient client = new DefaultHttpClient();
        //HttpPost post = new HttpPost(url);
        Log.d("POST Url", url);
        MultipartEntity mpEntity = new MultipartEntity();
        //Path of the file to be uploaded
        //String filepath = "";
        File file = new File(filePath);
        Log.d("POST File", filePath);
        ContentBody cbFile = new FileBody(file, "image/jpeg");         

        //Add the data to the multipart entity
        mpEntity.addPart("file", cbFile);
        mpEntity.addPart("name", new StringBody("Test", Charset.forName("UTF-8")));
        //mpEntity.addPart("data", new StringBody("This is test report", Charset.forName("UTF-8")));
        post.setEntity(mpEntity);
        //Execute the post request
        Log.d("POST Step", "Executing");
        HttpResponse response1 = client.execute(post);
        Log.d("POST Step", "Executed");
        //Get the response from the server
        HttpEntity resEntity = response1.getEntity();
        String Response=EntityUtils.toString(resEntity);
        Log.d("Response:", Response);
        //Generate the array from the response
        JSONArray jsonarray = new JSONArray("["+Response+"]");
        JSONObject jsonobject = jsonarray.getJSONObject(0);
        //Get the result variables from response 
        String result = (jsonobject.getString("result"));
        String msg = (jsonobject.getString("msg"));
        //Close the connection
        client.getConnectionManager().shutdown();
    }
}
